﻿using com.yoozoo.gta.Gameplay.RTS;
using TMPro;
using UnityEngine;
using XLua;
using Yoozoo.Framework.Managers;
using Yoozoo.Gta.Common;

namespace Yoozoo.Gameplay.RTS
{
    public class WorldBossHud : HudBase
    {
        private GameObject Lod0;
        private GameObject Lod1;
        private GameObject txtRoot;
        private TextMeshPro txtDesc;
        private GameObject IconRoot;
        private GameObject IconRootLod1;
        private GameObject IconBgLod1;
        private GameObject NewRecordEffect;
        
        public override void Initialize(int team)
        {
            base.Initialize(team);
            var hudManager = ArmyManager.Instance.HudManager;

            if (Lod0 == null)
            {
                Lod0 = hudManager.GetHud(HudManager.WorldBossHud);
            }
            if (Lod1 == null)
            {
                Lod1 = hudManager.GetHud(HudManager.WorldBossHud);
            }

            if (NewRecordEffect == null)
            {
                NewRecordEffect = Lod0.transform.Find("sfx_ui_Hud_short").gameObject;
            }
            if (NewRecordEffect.activeSelf)
            {
                NewRecordEffect.SetActive(false);
            }

            if (txtRoot == null)
            {
                txtRoot = Lod0.transform.Find("bg").gameObject;
            }

            if (txtDesc == null)
            {
                txtDesc = Lod0.transform.Find("bg/txtDesc")?.GetComponent<TextMeshPro>();
            }
            IconRootLod1 = Lod1.transform.Find("Icon").gameObject;
            IconBgLod1 = Lod1.transform.Find("bg").gameObject;
            
            IconRoot = Lod0.transform.Find("Icon").gameObject;
            txtDesc.text = "";
        }
        public override void ForceShowSelectedUI(bool isShow, bool isBattleStyle)
        {
        }

        public override void OnCameraPosUpdated(MarchContext marchContext)
        {
            LodLevelChange(marchContext.LodLevel, marchContext.LodPercent, marchContext);
            if (_marchLodLevel <= WorldEntry.MaxShowMarchLodLevel)
            {
                UpdateLod0Scale(marchContext);
            }
        }
        private float _lodScale;
        private void UpdateLod0Scale(MarchContext context)
        {
            float newLod0Scale = context.Lod0Scale;
            // 更新Lod0
            if (Mathf.Abs(_lodScale - newLod0Scale) > 0.00001f)
            {
                _lodScale = newLod0Scale;
                if (Lod0 != null)
                {
                    //Lod0.transform.localScale = new Vector3(_lodScale, _lodScale, _lodScale);
                }
            }
        }
        /// <summary>
        /// 沙盒的LOD切换后，切换相应的LOD物件
        /// </summary>
        /// <param name="currentLodLevel"></param>
        private void OnLodLevelChange(int currentLodLevel)
        {
            // LOD <  2: LOD0
            // LOD >= 2: LOD1
            if (_marchLodLevel != currentLodLevel)
            {
                _marchLodLevel = currentLodLevel;
            }

        }
        private void LodLevelChange(int currentLodLevel, float currentLodPercent, MarchContext marchContext)
        {
            // 先处理LOD
            if (currentLodLevel != _marchLodLevel)
            {
                OnLodLevelChange(currentLodLevel);
                SetLod0Visible(marchContext);
            }

            if (Lod1)
            {
                if (currentLodLevel >= WorldEntry.MaxShowMarchLodLevel && currentLodLevel < WorldEntry.MaxShowMarchLodLevel + 1)
                {
                    Lod1.gameObject.SetActive(true);
                    IconRootLod1.gameObject.SetActive(true);
                    IconBgLod1.gameObject.SetActive(false);
                    NewRecordEffect.SetActive(false);
                }
                else
                {
                    Lod1.gameObject.SetActive(false);
                }
            }
        }
        protected bool _lod0Visible = false;
        protected virtual void SetLod0Visible(MarchContext marchContext, bool force = false)
        {
            bool isShowLod0 = marchContext.LodLevel < WorldEntry.MaxShowMarchLodLevel;
            bool lod0Visible = isShowLod0;
            if (_lod0Visible != lod0Visible || force)
            {
                if (!lod0Visible)
                {
                    Lod0.transform.localScale = Vector3.zero;
                }
                else
                {
                    base.SetHudLodScale(LodHudType.MonsterLodLowHudMax, Lod0, marchContext);
                }
                _lod0Visible = lod0Visible;

                Lod0.SetActive(_lod0Visible);
            }
        }
        public override void SetSelected(bool selected, bool isMultySelect)
        {
            
        }

        public override void UpdatePlayerName()
        {
        }

        protected override void InitDefaultTrigger()
        {
        }
        protected bool hasSetParent = false;
        private Vector3 lodOffset = new Vector3(-0.755f, 0.12f, -0.524f);
        protected override void InitParentScaleAndPosition()
        {
            if (!hasSetParent)
            {
                // Lod0.transform.SetParent(MarchEntity.Transform);
                // Lod0.transform.localPosition = new Vector3(-0.755f, 0.12f, -0.524f);
                //
                // Lod1.transform.SetParent(MarchEntity.Transform);
                // Lod1.transform.localPosition = new Vector3(-0.755f, 0.12f, -0.524f);
            }
            hasSetParent = true;
        }
        public override void UpdateFollowPos()
        {
           
            if (hasSetParent)
            {
                var parentPos = MarchEntity.Transform.position;
                var pos = lodOffset + parentPos;
                
                if (Lod0 != null)
                {
                    Lod0.transform.localPosition = pos;
                }

                if (Lod1 != null)
                {
                    Lod1.transform.localPosition = pos;
                }
                
            }

            // 0: icon 1: 伤害 2: 新纪录
            int showState = ResidentHandler.Inst.GetFunction("CityLuaFunction.getWorldBossShowTotalDamage").Func<int, int>(0);
            IconRoot.SetActive(showState==0);
            txtRoot.SetActive(showState!=0);
            switch (showState)
            {
                case 1:
                    {
                        // 伤害
                        txtDesc.text = ResidentHandler.Inst.GetFunction("CityLuaFunction.getWorldBossShowTotalDamageStr").Func<int, string>(0);
                    }
                    break;
                
                case 2:
                    {
                        // 新纪录
                        txtDesc.text = ResidentHandler.Inst.GetFunction("CityLuaFunction.getWorldBossShowNewRecordStr").Func<int, string>(0);
                        if (!NewRecordEffect.activeSelf)
                        {
                            NewRecordEffect.SetActive(true);
                        }
                    }
                    break;
            }
        }
        protected override void InitSoldierNumAndRage()
        {
        }

        protected override void OnLod0ScaleChanged(MarchContext marchContext)
        {
            SetHudLodScale(LodHudType.MonsterLodLowHudMax, Lod0, marchContext);
        }

        protected override void OnLod1ScaleChanged(MarchContext marchContext)
        {
            if (Lod1)
            {
                SetHudLodScale(LodHudType.MonsterLodHighHudMax, Lod1, marchContext);
            }
        }

        protected override void SelfInit()
        {
        }

        internal void SetEntityAndTroopData(MarchEntity marchEntity, RtsServerInfo serverInfo)
        {
            MarchEntity = marchEntity;
        }
        public override void Dispose()
        {
            base.Dispose();
            var hudManager = ArmyManager.Instance.HudManager;

            hudManager.RecoverHud(HudManager.WorldBossHud, Lod0);
            hudManager.RecoverHud(HudManager.WorldBossHud, Lod1);

            Lod0 = null;
            Lod1 = null;
        }
    }
}
